from keras.datasets import mnist
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
from keras.layers import Conv2D

(X_train,Y_train),(X_test,Y_test) = mnist.load_data()
# 60000*28*28
print(X_train.shape)

# 一般图像有rgb三通道  咱们是灰度图 所以是28x28x1
X_train = X_train.reshape(60000,28,28,1)/255.0
X_test = X_test.reshape(10000,28,28,1)/255.0
# 归一化操作

from keras.utils import to_categorical
# 了解下什么叫one-shot编码
Y_train = to_categorical(Y_train,10)
Y_test = to_categorical(Y_test,10)

#创建模型
model = Sequential()

from keras.layers import Conv2D
# 6 32->28
                # 卷积核数量 卷积核尺寸       挪动的步长
                # 输入形状             填充模式        激活函数
# model.add(Conv2D(filters=6,kernel_size=(5,5),strides=(1,1),
#                  input_shape=(28,28,1),padding='valid',activation='relu'))
model.add(Conv2D(filters=6,kernel_size=(5,5),strides=(1,1),
                 data_format='channels_last',padding='valid',activation='relu'))

from keras.layers import AveragePooling2D
# 28->14
model.add(AveragePooling2D(pool_size=(2,2)))
# 16 14->10

model.add(Conv2D(filters=16,kernel_size=(5,5),strides=(1,1),
                 padding='valid',activation='relu'))
# 输出是一个多分类问题 而不是二分类问题  sigmoid做不了 
# softmax多分类 且多个预测出来的数值是百分比 令其为1
model.add(AveragePooling2D(pool_size=(2,2)))

from keras.layers import Flatten

model.add(Flatten())

model.add(Dense(units=120,activation='relu'))
model.add(Dense(units=84,activation='relu'))
model.add(Dense(units=10,activation='softmax'))

#告诉keras使用xxx代价函数和随机梯度下降算法(SGD)
model.compile(loss='categorical_crossentropy',
            optimizer=SGD(learning_rate=0.05),# Learning rate
            metrics=['accuracy'])

#start training
model.fit(X_train, Y_train, epochs=300, batch_size=128)

# 损失 和 准确率 评估
loss,accuracy = model.evaluate(X_test,Y_test)

print(loss)
print(accuracy)

# predict function
# pres = model.predict(X)
# 同时绘制散点图和预测曲线
# plot_utils.show_scatter_surface(X,Y,model)

# print(model.getweight())


